<template>
  <div>
    <order-header title="生成订单">
      <template v-slot:tip>
        <span>请尽快下单，尽早收货></span>
      </template>
    </order-header>
    <div class="create-order">
      <div class="address-wrap">
        <div class="name">
          <span>{{ address.userName }} </span>
          <span>{{ address.userPhone }}</span>
        </div>
        <div class="address">
          {{ address.provinceName }} {{ address.cityName }} {{ address.regionName }} {{ address.detailAddress }}
        </div>
      </div>
      <div class="good">
        <div class="good-item" v-for="(item, index) in cartList" :key="index">
          <div class="good-img"><img :src="prefix(item.goodsCoverImg)" alt=""></div>
          <div class="good-desc">
            <div class="good-title">
              <span>{{ item.goodsName }}</span>
              <span>x{{ item.goodsCount }}</span>
            </div>
            <div class="good-btn">
              <div class="price">¥{{ item.sellingPrice }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="pay-wrap">
        <div class="price">
          <span>商品金额</span>
          <span>¥{{ total }}</span>
        </div>
        <el-button @click="createOrder" class="pay-btn" color="#1baeae" type="primary" block>生成订单</el-button>
      </div>
    </div>
      <el-dialog 
        :visible.sync="showPay"
        title="支付" 
        width="400px">
        <div class="pay-content">
          <div class="pay-item">
            <span>订单编号</span>
            <span>{{ orderNo }}</span>
          </div>
          <div class="pay-item">
            <span>支付金额</span>
            <span>¥{{ total }}</span>
          </div>
          <div class="pay-item">
            <el-radio :label="1" v-model="payType">支付宝支付</el-radio>
            <el-radio :label="2" v-model="payType">微信支付</el-radio>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancelOrder">取 消</el-button>
          <el-button type="primary" @click="pay">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import { getByCartItemIds } from '../service/cart'
import { getDefaultAddress, getAddressDetail } from '../service/address'
import { createOrder, payOrder } from '../service/order'
import { getLocal } from '@/util'
import OrderHeader from '../components/OrderHeader'

export default {
  components: {
    OrderHeader
  },
  data() {
    return {
      cartList: [],
      address: {},
      showPay: false,
      orderNo: '',
      cartItemIds: [],
      payType: 1,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      const { addressId, cartItemIds } = this.$route.query
      const _cartItemIds = cartItemIds ? JSON.parse(cartItemIds) : JSON.parse(getLocal('cartItemIds'))
      const { data: list } = await getByCartItemIds({ cartItemIds: _cartItemIds.join(',') })
      const { data: address } = addressId ? await getAddressDetail(addressId) : await getDefaultAddress()
      this.cartList = list
      this.address = address
    },
    async createOrder() {
      const params = {
        addressId: this.address.addressId,
        cartItemIds: this.cartList.map(item => item.cartItemId)
      }
      const { data, resultCode } = await createOrder(params)
      if(resultCode === 200) {
        this.$message.success('订单生成成功')
        this.orderNo = data
        this.showPay = true
      }
    },
    // 取消订单
    cancelOrder() {
      this.showPay = false
      this.$message.error('订单未支付！')
      this.$router.push({ path: 'order/list' })
    },
    async pay() {
      await payOrder({ orderNo: this.orderNo, payType: this.payType })
      this.showPay = false
      this.$message.success('支付成功！')
      this.$router.push({ path: 'order/list' })
    },
    prefix(url) {
      if (url && url.startsWith('http')) {
        return url
      } else {
        url = `http://localhost:8080${url}`
        return url
      }
    }
  },
  computed: {
    total: function() {
      let sum = 0
      this.cartList.forEach(item => {
        sum += item.goodsCount * item.sellingPrice
      })
      return sum
    }
  }
}
</script>

<style lang="less" scoped>
  @import '../common/style/mixin';
  .create-order {
    background: #f9f9f9;
    width: 50%;
    margin: 0 25%;
    .address-wrap {
      background: #fff;
      position: relative;
      font-size: 14px;
      padding: 15px;
      color: #222333;
      .name, .address {
        margin: 10px 0;
      }
      .arrow {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 20px;
      }
      &::before {
        position: absolute;
        right: 0;
        bottom: 0;
        left: 0;
        height: 2px;
        background: -webkit-repeating-linear-gradient(135deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
        background: repeating-linear-gradient(-45deg, #ff6c6c 0, #ff6c6c 20%, transparent 0, transparent 25%, #1989fa 0, #1989fa 45%, transparent 0, transparent 50%);
        background-size: 80px;
        content: '';
      }
    }
    .good-item {
      padding: 10px;
      background: #fff;
      display: flex;
      .good-img {
        img {
          .wh(100px, 100px)
        }
      }
      .good-desc {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;
        padding: 20px;
        .good-title {
          display: flex;
          justify-content: space-between;
        }
        .good-btn {
          display: flex;
          justify-content: space-between;
          .price {
            font-size: 16px;
            color: red;
            line-height: 28px;
          }
          .van-icon-delete {
            font-size: 20px;
            margin-top: 4px;
          }
        }
      }
    }
    .pay-wrap {
      width: 100%;
      background: #fff;
      padding: 10px 0;
      padding-bottom: 50px;
      border-top: 1px solid #e9e9e9;
      text-align: center;
      > div {
        display: flex;
        justify-content: space-between;
        padding: 0 5%;
        margin: 10px 0;
        font-size: 14px;
        span:nth-child(2) {
          color: red;
          font-size: 18px;
        }
      }
      .pay-btn {
        width: 200px;
        margin: 0 auto;
      }
    }
  }
  .pay-item {
    margin: 10px 0;
  }
</style>
